/** 
 * ================================================================================
 * 		Project		: Kompas Bola
 * 		File		: ListAdapter.java
 * 		Description	: List Adapter from Url
 * 		Called		: MainActivity.java
 * ================================================================================
 * */
package com.kompas.bola.application.adapter;

import java.util.List;
import com.kompas.bola.application.R;
import com.kompas.bola.application.WebActivity;
import com.kompas.bola.application.db.CrudHelper;
import com.kompas.bola.application.model.Content;
import com.kompas.bola.application.utils.Constants;
import com.kompas.bola.application.utils.ImageLoader;
import com.kompas.bola.application.utils.Utils;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ListAdapter extends BaseAdapter {
	/** =========================================================================== */  
	private Activity activity;
	private List<Content> data;
    private ImageLoader imageLoader;     
	private static LayoutInflater inflater = null;
	private boolean _sublist 			= false;
	private LinearLayout imageViewLayout;
	private WebView adBanner;
	private Intent intent;
	private String status;
	/** =========================================================================== */   
	public ListAdapter(Activity a, List<Content> d) {
		activity 						= a;
		data 							= d;
		inflater 						= (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader						= new ImageLoader(activity.getApplicationContext());
        setList(data);
	}
	/** =========================================================================== */
	public void setList(List<Content> list){
		this.data						= list;
	}
	/** =========================================================================== */
	public List<Content> isList(){
		return this.data;
	}
	/** =========================================================================== */
	public void setSublist(boolean paramBoolean){
	    this._sublist 					= paramBoolean;
	}
	/** =========================================================================== */  
	public boolean isSublist(){
	    return this._sublist;
	}
	/** =========================================================================== */   
	public int getCount() {
		return data.size();
	}
	/** =========================================================================== */   
	public Object getItem(int position) {
		return position;
	}
	/** =========================================================================== */   
	public long getItemId(int position) {
		return position;
	}
	/** =========================================================================== */   
	public View getView(int position, View convertView, ViewGroup parent) {
		View vi = convertView;

        Content bola				= new Content();
        bola 						= data.get(position); 
      	
      	CrudHelper db				= new CrudHelper(activity);
      	status						= db.getStatusByGuid(bola.getKEY_GUID());
        
        if(bola.isKEY_FIRST() == true){
			vi 						= inflater.inflate(R.layout.list_first_row, null);	

		    imageViewLayout 		= ((LinearLayout)vi.findViewById(R.id.thumbnail));
			TextView guid			= (TextView) vi.findViewById(R.id.guid); // guid
			TextView title 			= (TextView) vi.findViewById(R.id.title); // title
			TextView time 			= (TextView) vi.findViewById(R.id.time); // time
			ImageView  thumb_image 	= (ImageView) vi.findViewById(R.id.list_image); // thumb image
			adBanner				= (WebView) vi.findViewById(R.id.adBanner);
			
			guid.setText(bola.getKEY_GUID());
			title.setText(bola.getKEY_TITLE());
			time.setText(Utils.convertDateTimeToFormattedTime(bola.getKEY_PUB_DATE()));
			adBanner.loadData(Constants.ADS, "text/html", "UTF-8");
			//adBanner.setVisibility(View.VISIBLE);

	      	if(status.equalsIgnoreCase(Constants.AFTER_READ)){
	      		title.setTextColor(Color.GRAY);
	      	}
			
			if(!bola.getKEY_PHOTO_URL().equalsIgnoreCase(Constants.EMPTY)){
		        imageViewLayout.setVisibility(View.VISIBLE);
				imageLoader.DisplayImage(bola.getKEY_PHOTO_URL(),thumb_image,Constants.SCALETYPE_CENTER_CROP);				
			}else{
				imageViewLayout.setVisibility(View.GONE);
				imageLoader.clearCache();
			}

			adBanner.setOnTouchListener(new View.OnTouchListener() {
				@Override
				public boolean onTouch(View v, MotionEvent event) {  
		            intent			= new Intent(activity, WebActivity.class);
		            intent.putExtra(Constants.URL, Constants.ADS_URL);
		            ((Activity) activity).startActivityForResult(intent, 0);
		            
	               return true;
	            }
			});
		}else{

			vi 						= inflater.inflate(R.layout.list_row, null);	

		    imageViewLayout 		= ((LinearLayout)vi.findViewById(R.id.thumbnail));
			TextView guid			= (TextView) vi.findViewById(R.id.guid); // guid
			TextView title 			= (TextView) vi.findViewById(R.id.title); // title
			TextView time 			= (TextView) vi.findViewById(R.id.time); // time
			ImageView  thumb_image 	= (ImageView) vi.findViewById(R.id.list_image); // thumb image
			adBanner				= (WebView) vi.findViewById(R.id.adBanner);			

			adBanner.loadData(Constants.ADS, "text/html", "UTF-8");
			guid.setText(bola.getKEY_GUID());
			title.setText(bola.getKEY_TITLE());
			time.setText(Utils.convertDateTimeToFormattedTime((bola.getKEY_PUB_DATE() != null ? bola.getKEY_PUB_DATE() : Constants.EMPTY)));


	      	if(position % 10 == 0){
				//adBanner.setVisibility(View.VISIBLE);
			}
	      	
	      	if(!status.equalsIgnoreCase(Constants.BEFORE_READ)){
	      		title.setTextColor(Color.GRAY);
	      	}

			adBanner.setOnTouchListener(new View.OnTouchListener() {
				@Override
				public boolean onTouch(View v, MotionEvent event) {  
		            intent			= new Intent(activity, WebActivity.class);
		            intent.putExtra(Constants.URL, Constants.ADS_URL);
		            ((Activity) activity).startActivityForResult(intent, 0);
		            
	               return true;
	            }
			});
			
			if(!bola.getKEY_THUMB_URL().equalsIgnoreCase(Constants.EMPTY)){
		        imageViewLayout.setVisibility(View.VISIBLE);
				imageLoader.DisplayImage(bola.getKEY_THUMB_URL(),thumb_image,Constants.SCALETYPE_CENTER_CROP);				
			}else{
				imageViewLayout.setVisibility(View.GONE);
				imageLoader.clearCache();
			}
		}			
		
		return vi;
	}
	/** =========================================================================== */
}